//
//  SearchContactsViewController.m
//  Findif
//
//  Created by Mac on 2019/2/15.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "SearchContactsViewController.h"
#import "MyAttentionTableViewCell.h"
#import "MyFansTableViewCell.h"
#import "MyAttentionModel.h"
#import "AddGroupTableViewCell.h"
#import "ContactsTableViewHeaderView.h"
#import "GroupModel.h"
#import "GroupInfoViewController.h"
#import "PersonalViewController.h"

@interface SearchContactsViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UITextField * searchTextfiled;
@property (nonatomic, strong)NSArray<MyAttentionModel *> * attentionArray;
@property (nonatomic, strong)NSArray<MyAttentionModel *> * fansArray;
@property (nonatomic, strong)NSArray<GroupModel *> * groupArray;
//@property (strong, nonatomic)NSMutableArray *dataArray;
//@property (strong, nonatomic)NSMutableDictionary *dataDic;

@end

@implementation SearchContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xEFF0F1);
    
//    self.dataArray = [NSMutableArray array];
//    self.dataDic = [NSMutableDictionary dictionary];
    self.groupArray = [NSArray array];
    self.attentionArray = [NSArray array];
    self.fansArray = [NSArray array];
    
    [self setupNav];
    [self setupUI];
}

- (void)setAttentionArray:(NSArray<MyAttentionModel *> *)attentionArray{
    
    _attentionArray = attentionArray;
    
    [self.tableView reloadData];
}


- (void)setFansArray:(NSArray<MyAttentionModel *> *)fansArray{
    
    _fansArray = fansArray;
    
    [self.tableView reloadData];
}


- (void)setGroupArray:(NSArray<GroupModel *> *)groupArray{
    
    _groupArray = groupArray;
    
    [self.tableView reloadData];
}


- (void)setupNav{
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    //    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    //    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIButton * backButton1 = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0 , 0 , 65 * SCREENH6, 20 * SCREENH6);
    [backButton1 setTitle:@"取消" forState:UIControlStateNormal];
    backButton1.titleLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    [backButton1 setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    
    [backButton1 addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
}


- (void)setupUI{
    
    UITextField * searchTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 5 * SCREENH6, 260 * SCREENH6, 30 * SCREENH6)];
    
    searchTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    searchTextfiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    searchTextfiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xbebebe);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    searchTextfiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.string attributes:attrs];
    searchTextfiled.textColor = [UIColor blackColor];
    searchTextfiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 16 * SCREENH6, 17.5 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = [UIImage imageNamed:@"hg"];
    [rightView addSubview:image];
    searchTextfiled.leftView = rightView;
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.returnKeyType = UIReturnKeySearch;
    searchTextfiled.delegate = self;
    
    self.searchTextfiled = searchTextfiled;
    
    //    [view addSubview:self.searchTextfiled];
    
    self.navigationItem.titleView = self.searchTextfiled;
    
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xEFF0F1);
    tableView.rowHeight = 60 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
//    tableView.hidden = YES;

    self.tableView = tableView;

    [self.tableView registerClass:[AddGroupTableViewCell class] forCellReuseIdentifier:@"SearchGroupTableViewCell"];
    [self.tableView registerClass:[MyAttentionTableViewCell class] forCellReuseIdentifier:@"SearchMyAttentionTableViewCell"];
    [self.tableView registerClass:[MyFansTableViewCell class] forCellReuseIdentifier:@"SearchMyFansTableViewCell"];
    [self.tableView registerClass:[ContactsTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:@"SearchTableViewHeaderView"];

    //    self.messageTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self updateData];
    //    }];

    [self.view addSubview:self.tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    // 取出字典中的 section 如果是第 0 个分区 那么就返回该分区的数据
//    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",section]] isEqualToString:@"0"])
//    {
//        NSLog(@"count===%ld",[self.dataArray[section] count]);
//        return [self.dataArray[section] count];
//    }else
//    {
//        //            NSLog(@"**************");
//        return 0;
//    }
    if (section == 0) {

        return self.attentionArray.count;
    }else if (section == 1){

        return self.fansArray.count;
    }else{

        return self.groupArray.count;
    }
    
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){

        MyAttentionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchMyAttentionTableViewCell" forIndexPath:indexPath];

        cell.model = self.attentionArray[indexPath.row];


        return cell;
    }else if(indexPath.section == 1){

        MyFansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchMyFansTableViewCell" forIndexPath:indexPath];

        cell.model = self.fansArray[indexPath.row];

        return cell;
    }else{

        AddGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchGroupTableViewCell" forIndexPath:indexPath];

        cell.model = self.groupArray[indexPath.row];

        return cell;
    }
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ContactsTableViewHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SearchTableViewHeaderView"];
    
    header.status = @"show";
//    header.userInteractionEnabled = YES;
//    header.tag = section;
//    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//
//    [header addGestureRecognizer:tapGR];
    
    switch (section) {
        case 0:
            header.titleLabel.text = @"我的关注";
            header.countLabel.text = [NSString stringWithFormat:@"%lu",self.attentionArray.count];
            break;
        case 1:
            header.titleLabel.text = @"我的粉丝";
            header.countLabel.text = [NSString stringWithFormat:@"%lu",self.fansArray.count];
            break;
        case 2:
            header.titleLabel.text = @"我的群组";
            header.countLabel.text = [NSString stringWithFormat:@"%lu",self.groupArray.count];
            break;
        default:
            break;
    }
    
    return header;
}


- (void)tapAction:(UITapGestureRecognizer*)sender{
    
//    ContactsTableViewHeaderView * header = (ContactsTableViewHeaderView *)sender.view;
    
    //    header.statusImageView.image = [UIImage imageNamed:@"多边形下"];
//    NSInteger temp = header.tag;
//    if ([header.status isEqualToString:@"show"]) {
//
//        header.status = @"hidden";
//    }else if ([header.status isEqualToString:@"hidden"]){
    
//        header.status = @"show";
//    }
    
    //    NSLog(@"temp==%ld",header.tag);
    // 修改 每个区的收缩状态  因为每次点击后对应的状态改变 temp代表是哪个section
//    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",temp]]isEqualToString:@"0"] )
//    {
//        [self.dataDic setObject:@"1" forKey:[NSString stringWithFormat:@"%ld",temp]];
//    }else
//    {
//        [self.dataDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",temp]];
//    }
    
//    if (temp == 0) {
//
//        if (self.attentionArray.count > 0) {
//            // 更新 section
//            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
//        }
//    }else if (temp == 1){
//
//        if (self.fansArray.count > 0) {
//            // 更新 section
//            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
//        }
//    }else if (temp == 2){
//
//        if (self.groupArray.count > 0) {
//            // 更新 section
//            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
//        }
//    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ZRWhiteColor;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    if (indexPath.section == 2) {
        
        GroupModel * model = self.groupArray[indexPath.row];
        
        GroupInfoViewController * infoVC = [GroupInfoViewController new];
        
        infoVC.group_id = model.group_id;
        
        [self.navigationController pushViewController:infoVC animated:YES];
        
    }else if(indexPath.section == 0){
        
        MyAttentionModel * model = self.attentionArray[indexPath.row];
        
        PersonalViewController * personVC = [PersonalViewController new];
        
        personVC.ID = model.attention_user_id;
        
        [self.navigationController pushViewController:personVC animated:YES];
        
    }else if(indexPath.section == 1){
        
        MyAttentionModel * model = self.fansArray[indexPath.row];
        
        PersonalViewController * personVC = [PersonalViewController new];
        
        personVC.ID = model.user_id;
        
        [self.navigationController pushViewController:personVC animated:YES];
    }
}


//实现UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    [[ZRNetworkTool sharedTool] searchContactsWithSearch_str:textField.text WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            self.attentionArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:dict[@"attention"]];
            //        NSLog(@"attentionArray===%@",self.attentionArray);
            self.fansArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:dict[@"fans"]];
            //        NSLog(@"fansArray===%@",self.fansArray);
            self.groupArray = [NSArray yy_modelArrayWithClass:[GroupModel class] json:dict[@"group"]];
            //        NSLog(@"group===%@",self.groupArray);
        }
        
    }];
    
    return YES;
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
